package com.example.badminton.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.badminton.entity.News;
import com.example.badminton.mapper.NewsMapper;
import com.example.badminton.service.NewsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
    
    @Override
    public List<News> getNewsList(String category, String keyword) {
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1); // 只查询已发布的资讯
        
        if (StringUtils.hasText(category) && !"all".equals(category)) {
            queryWrapper.eq("category", category);
        }
        
        if (StringUtils.hasText(keyword)) {
            queryWrapper.and(wrapper -> wrapper
                .like("title", keyword)
                .or()
                .like("description", keyword)
                .or()
                .like("content", keyword)
            );
        }
        
        queryWrapper.orderByDesc("create_time");
        return list(queryWrapper);
    }
    
    @Override
    public News getNewsDetail(Long id) {
        return getById(id);
    }
    
    @Override
    public void increaseViews(Long id) {
        News news = getById(id);
        if (news != null) {
            news.setViews(news.getViews() == null ? 1 : news.getViews() + 1);
            updateById(news);
        }
    }
    
    @Override
    public void toggleLike(Long id) {
        News news = getById(id);
        if (news != null) {
            news.setLikes(news.getLikes() == null ? 1 : news.getLikes() + 1);
            updateById(news);
        }
    }
} 